package com.design.template;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 模板方法bean监听器
 *
 * @author Zee
 * @date 2021年8月27日
 */
@Component

public class TemplateListener implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        Map<String, Object> beans = event.getApplicationContext().getBeansWithAnnotation(Template.class);
        TemplateContext templateContext = event.getApplicationContext().getBean(TemplateContext.class);
        beans.forEach((name, bean) -> {
            Template template = bean.getClass().getAnnotation(Template.class);
            templateContext.setTemplate(template.value().code, (AbstractTemplate) bean);
        });
    }
}
